<template>
  <div class="table-wrapper">
    <div class="search-container">
      <div class="search-wrapper">
        <span class="label">楼宇：</span>
        <el-cascader size="mini" class="large-select" v-model="roomIds" :options="buildingList" placeholder="请选择楼宇"
                     :props="{ checkStrictly: true, emitPath: false, multiple: false }"
                     collapse-tags
                     clearable
                     @expand-change="expandChange"
                     @change="geneParams">
        </el-cascader>
      </div>
      <div class="search-wrapper">
        <span class="label">时间类型：</span>
        <el-select size="mini" v-model="params.time_type" placeholder="请选择时间类型" @change="changeTimeType">
          <el-option v-for="item in timeTypeList" :key="item.text" :label="item.text" :value="item.value"></el-option>
        </el-select>
      </div>
      <div class="search-wrapper">
        <span class="label">开始时间：</span>
        <el-date-picker
          size="mini"
          v-model="params.start_time"
          :type="params.time_type===1?'date':(params.time_type===2?'month':'year')"
          :value-format="params.time_type===1?'yyyy-MM-dd':(params.time_type===2?'yyyy-MM':'yyyy')"
          placeholder="选择开始时间"
        >
        </el-date-picker>
      </div>
      <div class="search-wrapper">
        <span class="label">结束时间：</span>
        <el-date-picker
          size="mini"
          v-model="params.end_time"
          :type="params.time_type===1?'date':(params.time_type===2?'month':'year')"
          :value-format="params.time_type===1?'yyyy-MM-dd':(params.time_type===2?'yyyy-MM':'yyyy')"
          placeholder="选择结束时间"
        >
        </el-date-picker>
      </div>
      <div class="search-wrapper">
        <el-button @click="getRoomChartList" icon="el-icon-search" size="mini" >查询</el-button>
      </div>
    </div>
    <div class="content-container">
      <div class="box">总用电量: </div>
      <div class="box">房间总用电量: </div>
      <div class="box">房间电量占比: </div>
      <div class="box">房间平均用电: </div>
    </div>
    <div class="contrast-container">
      <div class="left">
        <common-table size="small" :data="data"></common-table>
      </div>
      <div class="right">
        <common-table size="small" :data="data1"></common-table>
      </div>
    </div>
  </div>
</template>

<script>
import portal from '@/api/portal'
import CommonTable from '@/views/pages/components/CommonTable'

export default {
  name: 'FjRatio',
  components: {
    CommonTable
  },
  data () {
    return {
      params: {
        start_time: null,
        end_time: null,
        time_type: null,
        room: null
      },
      buildingList: [],
      roomIds: [],
      roomMap: {},
      timeTypeList: [
        {
          text: '天',
          value: 1
        },
        {
          text: '月',
          value: 2
        },
        {
          text: '年',
          value: 3
        }
      ],
      data: {
        name: '最高用电',
        data: [
        ]
      },
      data1: {
        name: '最低用电',
        data: [
        ]
      }
    }
  },
  created () {
    this.getRoomClass()
  },
  computed: {
  },
  mounted () {
  },
  methods: {
    // 房间用的查询列表
    async getRoomChartList () {
      if (!this.params.room) {
        this.$message.warning('请选择房间')
        return
      }
      if (!this.params.time_type) {
        this.$message.warning('请选择时间类型')
        return
      }
      if (!this.params.start_time) {
        this.$message.warning('请选择开始时间')
        return
      }
      if (!this.params.end_time) {
        this.$message.warning('请选择结束时间')
        return
      }
      const params = Object.assign({}, this.params)
      const result = await portal.getRatio(params)
      if (result.data) {
        this.data = result.data
      }
    },
    // 获取楼宇
    async getRoomClass () {
      const result = await portal.getRoomClass()
      if (result.data) {
        const building = result.data
        building.forEach(e => {
          e.label = e.name
          e.value = e.id
          e.disabled = true
          e.children = e.child
          if (e.children) {
            e.children.forEach(el => {
              el.label = el.name
              el.value = el.id
              el.disabled = true
              el.children = el.child || []
              // delete el.children
              // if (el.children) {
              //   el.children.forEach(ele => {
              //     ele.label = el.name
              //     ele.value = el.id
              //     delete ele.children
              //   })
              // }
            })
          }
        })
        this.buildingList = building
      }
    },
    async expandChange (val) {
      if (val && val.length === 2) {
        let building
        this.buildingList.forEach(e => {
          if (e.children) {
            e.children.forEach(el => {
              if (el.value === val[1]) {
                building = el
              }
            })
          }
        })
        if (!building.loaded) {
          const params = {
            id: val[1]
          }
          const result = await portal.getRoomClassName(params)
          if (result.data) {
            const rooms = result.data
            rooms.forEach(e => {
              e.label = e.name
              e.value = e.id
              e.$parent = building
              e.disabled = false
              this.roomMap[e.id] = e
            })
            building.children = rooms
            building.loaded = true
          }
        }
      }
    },
    // 获取房间
    async geneParams () {
      const params = []
      if (this.roomIds && this.roomIds.length) {
        this.roomIds.forEach(e => {
          params.push(this.roomMap[e].$parent.id + '-' + this.roomMap[e].room_no)
        })
      }
      this.params.room = params.join('@')
    },
    changeTimeType () {
      this.params.start_time = null
      this.params.end_time = null
    },
    changePage (val) {
      this.params.page = val
      this.getRoomChartList()
    }
  }
}
</script>

<style scoped lang="scss">
.content-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  height: 60px;
  line-height: 60px;
  .box {
    flex: 1;
    text-align: center;
    height: 60px;
    line-height: 60px;
    font-size: 20px;
  }
}

.contrast-container {
  display: flex;
  justify-content: space-between;
  height: calc(100% - 150px);
  .left, .right {
    flex: 1;
    height: 100%;
    position: relative;
  }
}

.column {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 24px;
  padding: 30px 100px;
}

.header {
  font-weight: bold;
}

.room {
  font-size: 20px;
  margin-top: 10px;
}

</style>
